package com.example.teamtribe.database;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

public class ChattingBox extends DataSupport {

    private int realId;
    private int userId;
    private List<Chatting> chattingList = new ArrayList<>();

    public ChattingBox(int realId, int userId) {
        this.realId = realId;
        this.userId = userId;
    }

    public synchronized boolean saveOrUpdate() {
        return saveOrUpdate("realId = ?", realId + "");
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == this)
            return true;
        if (obj instanceof ChattingBox) {
            ChattingBox chattingBox = (ChattingBox) obj;
            return realId == chattingBox.realId;
        }
        return false;
    }

    public boolean add(Chatting chatting) {
        chatting.saveOrUpdate();
        return chattingList.add(chatting);
    }

    public int getRealId() {
        return realId;
    }

    public int getUserId() {
        return userId;
    }

    public List<Chatting> getChattingList() {
        return chattingList;
    }

    public Chatting getRecent() {
        if (chattingList.isEmpty()) {
            return null;
        }
        return chattingList.get(chattingList.size() - 1);
    }
}
